package cn.gz.lbs.main;

import org.apache.commons.configuration.CompositeConfiguration;
import org.apache.log4j.Logger;

import cn.gz.zoomtech.common.ConfManager;

import com.netnewera.l1.ans.Ans;
import com.netnewera.l1.ans.Lia;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
import net.sf.ehcache.config.CacheConfiguration;
import net.sf.ehcache.store.MemoryStoreEvictionPolicy;

public class LIACacheManager {
	
	private static LIACacheManager instance = null;
	private static final Logger log = Logger.getLogger(LIACacheManager.class);
	private CacheManager cacheManager = null;
	private Cache liaCache = null;
	private static CompositeConfiguration cf = ConfManager.getInstance().getConf();
	
	/**
	 * 获取LIACacheManager实例
	 * @return LIACacheManager
	 */
	public static synchronized LIACacheManager getInstance(){
		if (instance == null) instance = new LIACacheManager();
		return instance;
	}
	
	/**
	 * 
	 */
	private LIACacheManager(){
		log.info("begin inti LIACacheManager...");
		init();
		log.info("begin inti LIACacheManager_OK...");
	}
	
	/**
	 * 
	* package_name: cn.gz.lbs.main
	* file_name:    LIACacheManager.java
	* description:  初始化lIA的缓存管理器
	* 2013-6-27下午4:20:48
	* Author: chenhui
	 */
	private void init(){
		log.info("I am in init().");
		try{
			cacheManager =	CacheManager.getInstance();
			long cacheLiveSeconds = cf.getLong("cacheLiveSeconds",10);
			long cacheIdleSeconds = cf.getLong("cacheIdleSeconds",5);
//				create cache:
				int maxElements = 10000;
				Cache cache = new Cache(
						  new CacheConfiguration("LIACache", maxElements)
						    .memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LFU)
						    .eternal(false)
						    .timeToLiveSeconds(cacheLiveSeconds) // 存活时间
						    .timeToIdleSeconds(cacheIdleSeconds) //  闲置时间
						    .diskExpiryThreadIntervalSeconds(0));
				
				cacheManager.addCache(cache);
				
				liaCache = cacheManager.getCache("LIACache");
				
		}catch(Exception ex){
			ex.printStackTrace();
			log.warn("LIACacheManager_initErrorIs:",ex);
		}
		
	}
	
	
	/**
	 * 
	* package_name: cn.gz.lbs.main
	* file_name:    LIACacheManager.java
	* description: 停止cache服务。
	* 2013-6-27下午4:27:00
	* Author: chenhui
	 */
	public void shutDownCache(){
		try{
			cacheManager.shutdown();
		}catch(Exception ex){
			ex.printStackTrace();
			log.warn("shudDownCacheErrorIs:",ex);
		}
	}

	/**
	 * package_name: cn.gz.lbs.main
	 * file_name:    LIACacheManager.java
	 * description: 
	 * 2013-6-19下午3:59:37
	 * Author: chenhui
	 * @param args
	 */
	public static void main(String[] args) {

	}
	
	
	

	/**
	 * 
	* package_name: cn.gz.lbs.main
	* file_name:    LIACacheManager.java
	* description: 将键值对放入Lia的缓存管理器
	* 2013-6-27下午5:15:18
	* Author: chenhui
	 * @param key 构成：lcsClientId+tel
	 * @param lia
	 */
	public void putCache(String key,Ans ans){
		try{
			Element element = new Element(key, ans);
			liaCache.put(element);
		}catch(Exception ex){
			ex.printStackTrace();
			log.warn("whenPutCacheWarn:",ex);
		}
	}
	
	/**
	 * 
	* package_name: cn.gz.lbs.main
	* file_name:    LIACacheManager.java
	* description:  根据key从缓存中查询是否存在，不存在则返回null。
	* 2013-6-27下午5:14:32
	* Author: chenhui
	 * @param key
	 * @return Lia
	 */
	public Ans getCacheByKey(String key){
		Ans ans = null;		
		try{
			int elementsInMemory = liaCache.getSize();
			log.debug("elementsInMemory:"+elementsInMemory+",key is:"+key);
			Element elementOut = liaCache.getQuiet(key);
			log.info("keyInMemory:"+key+",elementOut:"+elementOut);
			if(elementOut!=null){ // 未过期

				boolean isExpired= liaCache.isExpired(elementOut);
				log.info("elementOut is过期:"+isExpired+",key is:"+key);
				
				ans = (Ans)elementOut.getObjectValue();
				log.info("key:"+elementOut+"ansIs:"+ans);
							
			}else{ // 已过期
				log.info("key:"+key+",isExpired-false.");
			}
			long hits = liaCache.getStatistics().cacheHitCount();		
			log.debug("key:"+key+",ans_inCache:"+ans+",hits:"+hits);
			
		}catch(Exception ex){
			ex.printStackTrace();
			log.warn("getCacheWarn:",ex);
		}
		
		return ans;
		 
	}

	public Cache getLiaCache() {
		return liaCache;
	}

	public void setLiaCache(Cache liaCache) {
		this.liaCache = liaCache;
	}

	public CacheManager getCacheManager() {
		return cacheManager;
	}

	public void setCacheManager(CacheManager cacheManager) {
		this.cacheManager = cacheManager;
	}

}
